package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.ProfitLossDetailsAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.cayman.view.ProfitLossDetailsDialog;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.ProfitLossInfo;
import com.lanlian.commonlib.bean.ReqProfitLossList;
import com.lanlian.commonlib.bean.RespProfitLossList;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.commonlib.util.OnEventID;
import com.lanlian.commonlib.util.OnEventUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/23 16:49
 *     desc   : 止盈止损明细
 *     version: 1.0
 * </pre>
 */
public class ProfitLossDetailsActivity extends BaseActivity implements View.OnClickListener {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;

    private ArrayList<ProfitLossInfo> mList = new ArrayList<>();
    private ProfitLossDetailsAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profit_loss_details, true);
        initView();
        setListeners();
        initAdapter();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("止盈止损明细");
        setRightImage(R.mipmap.documentary_icon_problem);
        mRecycleView = findViewById(R.id.recycle_view);
        mLayRefresh = findViewById(R.id.lay_refresh);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mImgAction.setOnClickListener(this);
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getProfitLossList();
            }
        });
    }

    private void initAdapter() {
        mAdapter = new ProfitLossDetailsAdapter(R.layout.adapter_profit_loss_details, mList);
        mRecycleView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                new ProfitLossDetailsDialog(ProfitLossDetailsActivity.this, mList.get(position)).show();
            }
        });
        mRecycleView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_details);
    }

    private void getProfitLossList() {
        VolleyRequestUtils.getInstance().getProfitLossList(this.toString(), new ReqProfitLossList(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespProfitLossList respProfitLossList = (RespProfitLossList) response;
                mList = respProfitLossList.getDataList();
                mAdapter.setNewData(mList);
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                showToast(baseResponse.getMsg());
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.img_action:
                OnEventUtils.onEvent(this, OnEventID.STOP_PNL_IMAGE);
                startActivity(new Intent(this, ProfitLossImageActivity.class));
                break;
            default:
                break;
        }
    }
}
